\insert 'ApplyMoves.oz'

%%
%% Task 3: Å finne flytt
%%

declare Find SplitTrain
fun {Find Xs Ys}
   case Ys of Y|_ then
      local Split Foran Bak Moves Na Nb NewXs NewYs in
	 Foran#Bak = {SplitTrain Xs Y}
	 	 
	 Na = 1 + {Length Bak}
	 Nb = {Length Foran}

	 NewXs = {Append Bak Foran}
	 NewYs = {Drop Ys 1}
	 
	 Moves = trackA(Na)|trackB(Nb)|trackA(~Na)|trackB(~Nb)|nil
	 {Append Moves {Find NewXs NewYs}}
      end
   else
      nil
   end
end

fun {SplitTrain VognListe Vogn}
   local Pos Hs Ts in
      Pos = {Position VognListe Vogn}
      Hs = {Take VognListe (Pos - 1)}
      Ts = {Drop VognListe Pos}
      Hs#Ts
   end
end


%declare TrackBefore TrackAfter Cs Ds Moves
%TrackBefore = state(main:[a b c d] trackA: nil trackB: nil)
%TrackAfter = state(main:[d b a c] trackA: nil trackB: nil)
%
%Moves = {Find TrackBefore.main TrackAfter.main}
%{Visualize {ApplyMoves TrackBefore Moves}}
